package com.th.supcom.pds.drugsupply.client.widget;

import java.util.List;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.pds.common.client.commom.PdsAuditPrescServiceFactory;
import com.th.supcom.pds.drugsupply.client.model.DrugInfoModel;

/**
 * 供药属性维护主面板.
 * 
 * 药品信息
 */
public class PdsMaintainMainPanel extends ContentPanel
{

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    /** 供药属性信息查询表单. */
    private DrugMaintainSearchPanel m_dmsDrugMaintainNorthPanel;

    /** 供药属性信息表格. */
    private DrugMaintainEditorGridPanel m_drugMaintainEditorGridPanel;
    private final static String ADD_FALG = "1";

    public PdsMaintainMainPanel ()
    {
        init ();
    }

    public void addInfo ()
    {// 新增
        final DrugInfoModel model = new DrugInfoModel ();
        model.setFlag (ADD_FALG);
        final ListStore <DrugInfoModel> mListStore = m_drugMaintainEditorGridPanel.getListStore ();
        mListStore.insert (model, 0);
        m_drugMaintainEditorGridPanel.getGrid ().startEditing (0, 0);
    }

    private void init ()
    {
        this.setHeaderVisible (false);
        this.setBodyBorder (false);
        setLayout (new BorderLayout ());

        // 初始化上部面板
        m_dmsDrugMaintainNorthPanel = new DrugMaintainSearchPanel ();
        final BorderLayoutData northData = new BorderLayoutData (LayoutRegion.NORTH, 90);
        northData.setMargins (new Margins (2, 2, 2, 2));
        add (m_dmsDrugMaintainNorthPanel, northData);
        // 初始化中心面板
        m_drugMaintainEditorGridPanel = new DrugMaintainEditorGridPanel ();
        final BorderLayoutData centerData = new BorderLayoutData (LayoutRegion.CENTER);
        centerData.setMargins (new Margins (0, 2, 2, 2));
        add (m_drugMaintainEditorGridPanel, centerData);

    }

    public void modifyInfo (String deptCode)
    {
        final List <DrugInfoModel> models = m_drugMaintainEditorGridPanel.getListStore ().getModels ();
        if (models.isEmpty ())
        {
            MessageBox.alert ("警告", "没有对数据进行修改", null);
            return;
        }
        for (final DrugInfoModel drugInfoModel : models)
        {

            if (drugInfoModel.getPhamName () == null)
            {
                MessageBox.alert ("警告", "请输入药品名称", null);
                return;
            }
        }
        PdsAuditPrescServiceFactory.getPdsPropertyMaintainService ().modifyPhamBasicInfo (models, deptCode, new AsyncCallback <Void> ()
        {
            @Override
            public void onFailure (final Throwable caught)
            {

                MessageBox.alert ("警告", caught.getMessage (), null);
            }

            @Override
            public void onSuccess (final Void result)
            {
                m_drugMaintainEditorGridPanel.refreshdrugMaintainGridData ();
                Info.display ("提示", "保存成功");
            }
        });

    }

    public void queryInfo ()
    {
        m_drugMaintainEditorGridPanel.setSearchModel (m_dmsDrugMaintainNorthPanel.getFormdata ());
        m_drugMaintainEditorGridPanel.loadGridData ();

    }

    public void refreshInfo ()
    {
        m_drugMaintainEditorGridPanel.refreshdrugMaintainGridData ();
    }

}